package data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;

public class Group extends AbstractData {

	private long id = -1;
	private long displayId;
	private long hotelId;
	private travel travelId;
	private String travelString;
	private Date date;
	private Package priceRate;
	private String priceRateString;
	private Guest guest;
	private String guestString;
	private boolean sellRoom;
	private String note;
	private List<Reservation> listResv;
	private String listResvString;
	private long span;
	private long extrabed;
	private String reason;
	private SourcesBusiness sources;
	private String sourcesString;
	private RoomType roomtype;
	private String roomtypeString;
	private List<Group> taskList;
	private String taskListString;
	private CreditCard credit;
	private String creditString;
	private List<Payment> paymentList;
	private String paymentListString;
	private List<Charge> chargeList;
	private String chargeListString;
	private List<Folio> folioList;
	private String folioListString;
	private Agency agencyId;
	private String agencyIdString;
	private List<AuditTrail> auditTrailsList;
	private String auditTrailsListString;
	private List<Charge> OtherChargeList;
	private String OtherChargeString;
	private String Objective;

	public Group() {}

	public Group(long id) {
		this.id = id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getId() {
		return id;
	}

	public long getDisplayId() {
		return displayId;
	}
	public void setDisplayId(long displayId) {
		this.displayId = displayId;
	}
	public List<Reservation> getListResv() {
		if ((listResv == null || listResv.size() == 0)
				&& !(listResvString == null || listResvString.isEmpty())) {
			String[] idOfReservation = SplitList(listResvString);
			List<Reservation> list = new ArrayList<Reservation>();
			for (String idReservation : idOfReservation) {
				Reservation c = new Reservation(Long.parseLong(idReservation
						.trim()));
				list.add(c);
			}

			for (Reservation c : list)
				c.LoadData();

			listResv = list;
		}
		return listResv;
	}

	public void setListResv(List<Reservation> listResv) {
		this.listResvString = CombineList(listResv);
		this.listResv = listResv;
	}

	public String getListResvString() {
		return listResvString;
	}

	public void setListResvString(String listResvString) {
		this.listResvString = listResvString;
	}

	public long getHotelId() {
		return hotelId;
	}

	public void setHotelId(long hotelId) {
		this.hotelId = hotelId;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Package getPriceRate() {
		if (priceRate == null
				&& !(priceRateString == null || priceRateString.isEmpty())) {
			Package p = new Package(Long.parseLong(priceRateString));
			p.LoadData();
			priceRate = p;
		}
		return priceRate;
	}

	public void setPriceRate(Package priceRate) {
		this.priceRateString = String.format("%d", priceRate.getId());
		this.priceRate = priceRate;
	}

	public void setPriceRateString(String priceRateString) {
		this.priceRateString = priceRateString;
	}

	public Guest getGuest() {
		if (guest == null && !(guestString == null || guestString.isEmpty())&& !guestString.equals("0")) {
			Guest g = new Guest(Long.parseLong(guestString));
			g.LoadData();
			guest = g;
		}
		return guest;
	}

	public void setGuest(Guest guest) {
		this.guestString = String.format("%d", guest.getId());
		this.guest = guest;
	}

	public void setGuestString(String guestString) {
		this.guestString = guestString;
	}

	public boolean getSellRoom() {
		return sellRoom;
	}

	public void setSellRoom(boolean sellRoom) {
		this.sellRoom = sellRoom;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public long getSpan() {
		return span;
	}

	public void setSpan(long span) {
		this.span = span;
	}

	public long getExtrabed() {
		return extrabed;
	}

	public void setExtrabed(long extrabed) {
		this.extrabed = extrabed;
	}

	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	public SourcesBusiness getSources() {
		if (sources == null
				&& !(sourcesString == null || sourcesString.isEmpty())) {
			SourcesBusiness c = new SourcesBusiness(
					Long.parseLong(sourcesString));
			c.LoadData();
			sources = c;
		}
		return sources;
	}

	public void setSources(SourcesBusiness sources) {
		this.sourcesString = String.format("%d", sources.getId());
		this.sources = sources;
	}

	public void setSourcesString(String sourcesString) {
		this.sourcesString = sourcesString;
	}

	public RoomType getRoomtype() {
		if (roomtype == null
				&& !(roomtypeString == null || roomtypeString.isEmpty())) {
			RoomType rt = new RoomType(Long.parseLong(roomtypeString));
			rt.LoadData();
			roomtype = rt;
		}
		return roomtype;
	}

	public void setRoomtype(RoomType roomtype) {
		this.roomtypeString = String.format("%d", roomtype.getId());
		this.roomtype = roomtype;
	}

	public void setRoomtypeString(String roomtypeString) {
		this.roomtypeString = roomtypeString;
	}

	public List<Group> getTaskList() {
		if ((taskList == null || taskList.size() == 0)
				&& !(taskList == null || taskList.isEmpty())) {
			String[] idOfList = SplitList(taskListString);
			List<Group> list = new ArrayList<Group>();
			for (String id : idOfList) {
				Group a = new Group(Long.parseLong(id));
				list.add(a);
			}

			for (Group a : list)
				a.LoadData();

			taskList = list;
		}
		return taskList;
	}

	public void setTaskList(List<Group> taskList) {
		// this.taskList = taskList;
		this.taskListString = CombineList(taskList);
		this.taskList = taskList;
	}

	public String getTaskListString() {
		return taskListString;
	}

	public void setTaskListString(String taskListString) {
		this.taskListString = taskListString;
	}

	public travel getTravelId() {
		if (travelId == null
				&& !(travelString == null || travelString.isEmpty())) {
			if (travelString.equals("0")) {
				travelId = null;
			} else {
				travel r = new travel(Long.parseLong(travelString));
				r.LoadData();
				travelId = r;
			}
		}
		return travelId;
	}

	public void setTravelId(travel travelId) {
		this.travelString = String.format("%d", travelId.getId());
		this.travelId = travelId;
	}

	public CreditCard getCredit() {
		if (credit == null && !(creditString == null || creditString.isEmpty())) {
			if (creditString.equals("0")) {
				credit = null;
			} else {
				CreditCard r = new CreditCard(Long.parseLong(creditString));
				r.LoadData();
				credit = r;
			}
		}
		return credit;
	}

	public void setCredit(CreditCard credit) {
		this.creditString = String.format("%d", credit.getId());
		this.credit = credit;
	}

	public void setCreditString(String creditString) {
		this.creditString = creditString;
	}

	public String getCreditString() {
		return creditString;
	}

	public String getTravelString() {
		return travelString;
	}

	public void setTravelString(String travelString) {
		this.travelString = travelString;
	}

	public List<Payment> getPaymentList() {
		if ((paymentList == null || paymentList.size() == 0) && !(paymentListString == null || paymentListString.isEmpty()) && !paymentListString.equals("")) {
			String[] idOfList = SplitList(paymentListString);
			List<Payment> list = new ArrayList<Payment>();
			for (String id : idOfList) {
				Payment a = new Payment(Long.parseLong(id));
				list.add(a);
			}

			for (Payment a : list)
				a.LoadData();

			paymentList = list;
		}
		return paymentList;
	}

	public void setPaymentList(List<Payment> paymentList) {
		this.paymentListString = CombineList(paymentList);
		this.paymentList = paymentList;
	}

	public String getPaymentListString() {
		return paymentListString;
	}

	public void setPaymentListString(String paymentListString) {
		this.paymentListString = paymentListString;
	}

	public List<Charge> getChargeList() {
		if ((chargeList == null || chargeList.size() == 0) && !(chargeListString == null || chargeListString.isEmpty())) {
			String[] idOfList = SplitList(chargeListString);
			List<Charge> list = new ArrayList<Charge>();
			if(idOfList.length == 1 && idOfList[0].equals("")){
				chargeList = null;
			}else{
				for (String id : idOfList) {
					Charge a = new Charge(Long.parseLong(id.trim()));
					list.add(a);
				}
				for (Charge a : list)
					a.LoadData();
				chargeList = list;
			}
		
		}
		return chargeList;
	}

	public void setChargeList(List<Charge> chargeList) {
		this.chargeListString = CombineList(chargeList);
		this.chargeList = chargeList;
	}

	public String getChargeListString() {
		return chargeListString;
	}

	public void setChargeListString(String chargeListString) {
		this.chargeListString = chargeListString;
	}

	public List<Folio> getFolioList() {
		if ((folioList == null || folioList.size() == 0)
				&& !(folioListString == null || folioListString.isEmpty())) {
			String[] idOfList = SplitList(folioListString);
			List<Folio> list = new ArrayList<Folio>();
			for (String id : idOfList) {
				Folio a = new Folio(Long.parseLong(id));
				list.add(a);
			}

			for (Folio a : list)
				a.LoadData();

			folioList = list;
		}
		return folioList;
	}

	public void setFolioList(List<Folio> folioList) {
		this.folioListString = CombineList(folioList);
		this.folioList = folioList;
	}

	public String getFolioListString() {
		return folioListString;
	}

	public void setFolioListString(String folioListString) {
		this.folioListString = folioListString;
	}

	
	public Agency getAgencyId() {
		if (agencyId == null && !(agencyIdString == null || agencyIdString.isEmpty())) {
			if(agencyIdString == "0"){
				agencyId = null;
			}else{
				Agency r = new Agency(Long.parseLong(agencyIdString));
				r.LoadData();
				agencyId = r;
			}
			
		}
		return agencyId;
	}
	public String getAgencyIdString() {
		return agencyIdString;
	}

	public void setAgencyIdString(String agencyIdString) {
		this.agencyIdString = agencyIdString;
	}
	
	public List<AuditTrail> getAuditTrailsList() {
		//workaround by Newss
		if(auditTrailsListString == null)
			return new ArrayList<AuditTrail>();
		if ((auditTrailsList == null || auditTrailsList.size() == 0) && !(auditTrailsListString == null || auditTrailsListString.isEmpty())) {
			String[] ids = SplitList(auditTrailsListString);
			List<AuditTrail> list = new ArrayList<AuditTrail>();
			for (String id : ids) {
				AuditTrail a = new AuditTrail(Long.parseLong(id));
				list.add(a);
			} 
			for(AuditTrail a : list)
				a.LoadData();
			auditTrailsList = list;
		}
		return auditTrailsList;
	}

	public void addAuditTrails(AuditTrail auditTrail) {
		String tmp = CombineList(auditTrailsList);
		if(!tmp.isEmpty())
			tmp += ",";
		this.auditTrailsListString = tmp + auditTrail.getId();
		if(this.auditTrailsList == null)
			this.auditTrailsList = new ArrayList<AuditTrail>();
		this.auditTrailsList.add(auditTrail);
	}
	
	public List<Charge> getOtherChargeList() {
		if ((OtherChargeList == null || OtherChargeList.size() == 0) && (OtherChargeString != null)) {
			String[] idOfList = SplitList(OtherChargeString);
			List<Charge> list = new ArrayList<Charge>();
			for (String id : idOfList) {
				Charge a = new Charge(Long.parseLong(id));
				list.add(a);
			}
			for(Charge a : list)
				a.LoadData();
			OtherChargeList = list;
		}
		return OtherChargeList;
	}

	public void setOtherChargeList(
			List<Charge> OtherChargeList) {
		this.OtherChargeString =CombineList(OtherChargeList);
		this.OtherChargeList = OtherChargeList;
	}

	public String getOtherChargeString() {
		return OtherChargeString;
	}

	public void setOtherChargeString(String OtherChargeString) {
		this.OtherChargeString = OtherChargeString;
	}
	
	public String getObjective() {
		return Objective;
	}

	public void setObjective(String Objective) {
		this.Objective = Objective;
	}

	@Override
	public Entity getEntity() {
		Entity item;

		if (this.key == null)
			item = new Entity(EntityName.Group);
		else
			item = new Entity(this.key);

		item.setProperty("displayId", this.displayId);
		item.setProperty("hotelId", this.hotelId);
		item.setProperty("date", this.date);
		item.setProperty("priceRateId", this.priceRateString);
		item.setProperty("GuestId", this.guestString);
		item.setProperty("sellRoom", this.sellRoom);
		item.setProperty("note", this.note);
		item.setProperty("listResv", this.listResvString);
		item.setProperty("span", this.span);
		item.setProperty("extrabed", this.extrabed);
		item.setProperty("reason", this.reason);
		item.setProperty("sources", this.sourcesString);
		item.setProperty("taskList", this.taskListString);
		item.setProperty("travelId", this.travelString);
		item.setProperty("creditCard", this.creditString);
		item.setProperty("roomtype", this.roomtypeString);
		item.setProperty("paymentList",this.paymentListString);
		item.setProperty("chargeList",this.chargeListString);
		item.setProperty("folioList",this.folioListString);
		item.setProperty("agencyId", this.agencyIdString);
		item.setProperty("auditTrailsList", this.auditTrailsListString);
		item.setProperty("OtherCharge",this.OtherChargeString);
		item.setProperty("Objective",this.Objective);
		return item;
	}

	@Override
	public void setEntity(Entity entity) {

		this.id = entity.getKey().getId();
		this.displayId = (Long) entity.getProperty("displayId");
		this.hotelId = (Long) entity.getProperty("hotelId");
		this.date = (Date) entity.getProperty("date");
		this.priceRateString = (String) entity.getProperty("priceRateId");
		this.guestString = (String) entity.getProperty("GuestId");

		if (entity.getProperty("sellRoom") == null
				|| entity.getProperty("sellRoom") == "") {
			this.sellRoom = false;
		} else {
			this.sellRoom = (Boolean) entity.getProperty("sellRoom");
		}

		this.note = (String) entity.getProperty("note");
		this.listResvString = (String) entity.getProperty("listResv");
		if (entity.getProperty("span") == null
				|| entity.getProperty("span") == "") {
			this.span = 0;
		} else {
			this.span = (Long) entity.getProperty("span");
		}
		if (entity.getProperty("extrabed") == null
				|| entity.getProperty("extrabed") == "") {
			this.extrabed = 0;
		} else {
			this.extrabed = (Long) entity.getProperty("extrabed");
		}

		this.reason = (String) entity.getProperty("reason");
		this.sourcesString = (String) entity.getProperty("sources");
		this.taskListString = (String) entity.getProperty("taskList");
		this.travelString = (String) entity.getProperty("travelId");
		this.creditString = (String) entity.getProperty("creditCard");
		this.roomtypeString = (String) entity.getProperty("roomtype");
		this.paymentListString = (String) entity.getProperty("paymentList");
		this.chargeListString = (String) entity.getProperty("chargeList");
		this.folioListString = (String) entity.getProperty("folioList");
		this.agencyIdString = (String) entity.getProperty("agencyId");
		this.auditTrailsListString = (String) entity.getProperty("auditTrailsList");
		this.OtherChargeString=(String) entity.getProperty("OtherCharge");
		this.Objective =(String) entity.getProperty("Objective");
		this.key = entity.getKey();
	}

	public void LoadData() {
		if (this.id > -1) {
			Entity entity = LoadData(EntityName.Group, this.id);
			setEntity(entity);
		}
	}

}
